<template>
  <div>
    <MyHeader bgColor="orange" fontColor="green" title="我的购物车" />
    <div class="list" style="padding: 45px 0 50px">
      <MyGoods v-for="obj in list" :key="obj.id" :goodsList="obj" />
    </div>
    <MyFooter @toggle-all="isToggleAll" :isAllChecked="isAllSelected" :allPrice="countPrice" :allCount="totalCount"/>
  </div>
</template>

<script>
import MyHeader from "./components/MyHeader";
import MyGoods from "./components/MyGoods";
import MyFooter from "./components/MyFooter";
export default {
    computed: {
        isAllSelected(){
        return this.list.every(item =>item.goods_state)
    },
    countPrice(){
        return this.list.reduce((sum,val) =>{
            if(val.goods_state){
                sum+=val.goods_count
            }
            return sum
        },0)
    },
    totalCount(){
        return this.list.reduce((sum,val) =>{
            if(val.goods_state){
                sum+=val.goods_count*val.goods_price
            }
            return sum
        },0)
    }
    },
  methods: {
    isToggleAll(checked) {
      this.list.forEach((item) => (item.goods_state = checked));
    },
   
  },
  data() {
    return {
      list: [],
    };
  },
  async created() {
    const resp = await this.$http({
      method: "GET",
      url: "/api/cart",
    });
    this.list = resp.data.list;
    console.log(this.list);
  },
  components: {
    MyHeader,
    MyGoods,
    MyFooter,
  },
};
</script>

<style>
</style>